﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class complainEdit : BasePage
    {
        WTODataContext contex = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPageControl();
            }
            this.btnDelete.Attributes.Add("onclick", "return confirm('您确定要删除该投诉记录吗?');");
        }

        private void InitPageControl()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int cid = int.Parse(Request.QueryString["id"]);
                var com = contex.Complaints.SingleOrDefault(p => p.ID == cid);
                if (com != null)
                {
                    this.lblTitle.Text = com.title;
                    lblDatetime.Text = com.created_date.ToString();
                    litContent.Text = com.question;
                    this.txtFeedBack.Text = com.answer;

                    if (com.record_status != 'N')
                    {
                        this.ddlOperate.Items.FindByValue(com.record_status.ToString()).Selected = true;
                    }

                    if (com.record_status == 'P')
                    {
                        this.txtFeedBack.Enabled = false;
                        this.btnOK.Visible = false;
                    }
                    else
                    {
                        this.txtFeedBack.Enabled = true;
                        this.btnOK.Visible = true;
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "paramerr", "alert('参数传递失败');location='ManageComplain.aspx';", true);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int cid = int.Parse(Request.QueryString["id"]);
                var com = contex.Complaints.SingleOrDefault(p => p.ID == cid);
                if (com != null)
                {
                    if (this.txtFeedBack.Text.Trim() == "" && this.ddlOperate.SelectedItem.Value == "P")
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "paramerr", "alert('请填写处理意见');", true);
                        return;
                    }
                    com.record_status = char.Parse(ddlOperate.SelectedItem.Value);
                    com.replier = Session["login_name"].ToString();
                    com.update_date = DateTime.Now;
                    com.answer = this.txtFeedBack.Text.Trim();
                    contex.SubmitChanges();
                    Response.Redirect("ManageComplain.aspx");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "paramerr", "alert('参数传递失败');location='ManageComplain.aspx';", true);
            }
        }

        protected void btnDelete_Click(object sender,EventArgs e)
        {
            int cid = int.Parse(Request.QueryString["id"]);
            var com = contex.Complaints.SingleOrDefault(p => p.ID == cid);
            if (com != null)
            {
                contex.Complaints.DeleteOnSubmit(com);
                contex.SubmitChanges();
                Response.Redirect("ManageComplain.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa1", "alert('参数传递失败');", true);
            }
        }
    }
}